package org.rcloud.io.service.impl;

import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.mapper.ArticleTable010MappingMapper;
import org.rcloud.io.mapper.TravelMapper;
import org.rcloud.io.mapper.TravelSiteMapper;
import org.rcloud.io.model.ArticleTable010Mapping;
import org.rcloud.io.model.Travel;
import org.rcloud.io.model.TravelSite;
import org.rcloud.io.service.TravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2019/3/12.
 */
@Service(value="TravelService")
public class TravelServiceImpl implements TravelService{
    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelSiteMapper tsm;

    @Autowired
    private ArticleTable010MappingMapper atmm;

    @Override
    public PageVo<Travel> findTravel() {

        try {
            List<Travel> list = travelMapper.selectAll();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo findByEid(String eid) {

        try {
            Travel travel = travelMapper.selectByEid(eid);
            return ResultVo.setOK(travel);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo savetravel(Travel travel) {

        if (travel.equals("") || travel == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                travelMapper.insertSelective(travel);
                return ResultVo.setOK(null);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }

    }

    @Override
    public ResultVo findById(int id) {

        try {
            Travel travel = travelMapper.selectByPrimaryKey(id);
            return ResultVo.setOK(travel);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo updateTravel(Travel travel) {

        try {
            travelMapper.updateByPrimaryKeySelective(travel);
            return ResultVo.setOK(travel);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public PageVo<TravelSite> findTravelSite() {
        try {
            List<TravelSite> list = tsm.findAll();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo findTravelSiteByEid(String eid) {
        if (eid.equals("") || eid == null){
            return ResultVo.setError("eid为空");
        } else {
            try {
                TravelSite travelSite = tsm.findByEid(eid);
                return ResultVo.setOK(travelSite);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }

    @Override
    public ResultVo saveTravelSite(TravelSite travelSite) {
        if (travelSite.equals("") || travelSite == null){
            return ResultVo.setError("添加数据为空");
        } else {
            tsm.insert(travelSite);
            return ResultVo.setOK(null);
        }
    }

    @Override
    public ResultVo updateTravelSite(TravelSite travelSite) {
        if (travelSite.equals("") || travelSite == null){
            return ResultVo.setError("修改数据为空");
        } else {
            tsm.updateByPrimaryKeySelective(travelSite);
            return ResultVo.setOK(null);
        }
    }

    @Override
    public ResultVo findTravelSiteById(int id) {
        try {
            TravelSite travelSite = tsm.selectByPrimaryKey(id);
            return ResultVo.setOK(travelSite);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }
    }


    @Override
    public ResultVo savaMapping(ArticleTable010Mapping articleTable010Mapping) {

        if (articleTable010Mapping.equals("") || articleTable010Mapping == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            Integer aId = articleTable010Mapping.getaId();
            ArticleTable010Mapping byAId = null;
            try {
                byAId = atmm.findByAId(aId);

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (byAId != null) {
                    articleTable010Mapping.setId(byAId.getId());
                    atmm.updateByPrimaryKey(articleTable010Mapping);
                    return ResultVo.setOK("修改成功");
                } else {
                    atmm.insert(articleTable010Mapping);
                    return ResultVo.setOK("添加成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }

}
